#导包
import logging

#定义一个对外的函数:log
# def log():
#     #1.1 使用basicConfig配置日志
#     logging.basicConfig(level=logging.DEBUG,
#                         #filename = "log1.text"   #这里会输出一个文件
#                         format="日志:%(name)s-级别:%(levelname)s-时间%(asctime)s-模块%(module)s.py-第%(lineno)d行:%(message)s")
#     #1.2获取一个日志记录器
#     logger = logging.getLogger("Test")
#     #1.3返回日志记录器给外界使用
#     return logger
import os

log_path = os.path.dirname(os.path.dirname(__file__)) + r"/testLog/"
def log():
    logger = logging.getLogger("Test")
    logger.setLevel(logging.DEBUG)
    format = logging.Formatter("日志:%(name)s-级别:%(levelname)s-时间%(asctime)s-模块%(module)s.py-第%(lineno)d行:%(message)s")
    sh = logging.StreamHandler()
    sh.setFormatter(format)
    logger.addHandler(sh)

    fh = logging.FileHandler(log_path + r"test.txt",encoding="utf-8")
    fh.setFormatter(format)
    logger.addHandler(fh)

    return logger

#单例模式
logger = log()

#
if __name__ == '__main__':
    logger = log()
    logger.debug("我是debug级别")
    logger.info("我是info级别")
    logger.critical("我是灾难")